<?php

namespace hotel\common\models;

use Yii;

/**
 * This is the model class for table "questionsss".
 *
 * @property int $id
 * @property string $appid
 * @property string $title
 * @property string $answer
 * @property string $create_time
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'comment';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_at','update_at'], 'safe'],
            [['appid','face','head_img','star'], 'string', 'max' => 255],
            [['content', 'reply','tags','comment_img','star_json'], 'string'],
            [['is_top','is_active','order_id'], 'number'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'appid' => 'Appid',
            'create_at' => 'create_at',
            'update_at' => 'update_at',
            'face' => 'face',
			'head_img' => 'head_img',
			'content' => 'content',
			'reply' => 'reply',
			'tags' => 'tags',
			'comment_img' => 'comment_img',
			'is_top' => 'is_top',
			'is_active' => 'is_active',
			'order_id' => 'order_id',
			'star' => 'star',
			'star_json' => 'star_json',
			
        ];
    }

    /**
     * @param $arr
     * @return bool
     * 插入数据
     */
    public static function setData($arr){
        $model = new self();
        $model->setAttributes($arr);
        if($model->save()){
            return false;
        }else{
			return $model->errors;
		}
        
    }

    /**
     * @param $params
     * @return mixed
     */
    public static function lists($params){
        $page = 1;
        $limit = 10;
		$appid = '';
		$tag = '';
        extract($params);
        //分页
        $offset = ($page - 1) * $limit;
        $model = self::find()
			->with(['orders'])
			->where(['appid'=>$appid,'is_active'=>1])
			->andFilterWhere(['like','tags',$tag]);
        $total	= $model->count();
        $data = $model->offset($offset)->limit($limit)->orderBy(['is_top'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
		
		foreach($data as $k=>$v){
			$data[$k]['face'] = $v['face']?base64_decode($v['face']):'';
			$data[$k]['star_json'] = json_decode($v['star_json']);
			$data[$k]['comment_img'] = explode(',',$v['comment_img']);
			$data[$k]['tags'] = explode(',',$v['tags']);
			$data[$k]['create_at'] = substr($v['create_at'],0,10);
			$data[$k]['orders']['arr'] = substr($v['orders']['arr'],0,10);
		}

        $res['list'] = $data;
        $res['total'] = $total;
        return $res;
    }
	
	/**
	 * @param $params
	 * @return mixed
	 */
	public static function listsBack($params){
	    $page = 1;
	    $limit = 10;
		$appid = '';
		$is_active='';
		$content='';
	    extract($params);
	    //分页
	    $offset = ($page - 1) * $limit;
	    $model = self::find()
			->where(['appid'=>$appid]);
		if($is_active===0){
			$model->andWhere(['is_active'=>0]);
		}else{
			$model->andFilterWhere(['is_active'=>$is_active]);
		}
		$model->andFilterWhere(['like','content',$content]);
	    $total	= $model->count();
	    $data = $model->offset($offset)->limit($limit)->orderBy(['is_top'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
		
		foreach($data as $k=>$v){
			$data[$k]['face'] = $v['face']?base64_decode($v['face']):'';
			$data[$k]['star_json'] = json_decode($v['star_json']);
			$data[$k]['comment_img'] = explode(',',$v['comment_img']);
			$data[$k]['tags'] = explode(',',$v['tags']);
		}
	
	    $res['list'] = $data;
	    $res['total'] = $total;
	    return $res;
	}
	
	public function getOrders()
	{
	    return $this->hasOne(Order::className(),['id'=>'order_id']);
	}
}
